/* number is lucky if sum of its digits is prime and sum of squared digits is also prime*/

#include <iostream>
using namespace std;

#define MAX_INT 1600
bool PRIME_TABLE[MAX_INT];

void genPrimes() {
	for(int i = 0; i < MAX_INT; ++i) {
	       PRIME_TABLE[i] = 1;
	}
	PRIME_TABLE[0] = PRIME_TABLE[1] = 0;

	for(int i = 2; i * i < MAX_INT; ++i) {
		if (! PRIME_TABLE[i])
			continue;
			
		for (int j = 2; j * i < MAX_INT; ++j) {
			PRIME_TABLE[j * i] = 0;
		}
	}
}

bool isLucky(int num) {
	int sum = 0;
	int sumSq = 0;
	int digit;
	while(num) {
		digit = num % 10;
		sum += digit;
		sumSq += digit*digit;
		num /= 10;
	}
	
	return PRIME_TABLE[sum] && PRIME_TABLE[sumSq];
}

int main() {
	
	genPrimes();
	cout << "input range: " << endl;
	int min,max;
	cin >> min >> max;

	int count = 0;
	for (int i = min; i <= max; ++i) {
		if(isLucky(i)) ++count; 

	}
	cout << count;
	return 0;
}	
	
